
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
             function validate(e)
            {        
                
                //get the value of the element paper          
                text = document.getElementById("paper").value;
                //get the last index of the "\"
                var indexOfSlash = text.lastIndexOf('\\');
                //get the file name which is the after the last "\" and set it to a variable
                var file = text.substring(indexOfSlash + 1);
                
                //                alert(text);
                //                return false;
//                arr=[","," ",";","'",">","<", ":","/","\\","?"];
//                for(i=0; i<arr.length; i++ )
//                {                        
//                    //                        alert(text.indexOf(arr[i]));
//                    //                        alert(text.indexOf(arr[i]) != -1);
//                    if(file.indexOf(arr[i]) != -1)
//                    {
//                        alert ("Warning! Illegal filename!");
//                        return false;
//                    }
//                    //                            break;
//                }
//                return true;
       //check if there is any illegal character in the file name using regular expressions then print a message for the user
                if(file.match(/^[\w\.]+$/) == null) {
                    alert ("Warning! Illegal filename!");
                    return false;
                }
                return true;
            }

        </script>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>

        <form action= ""  method="post" enctype="multipart/form-data"  onsubmit="return validate(this)" >
            <input type="file" name="paper" accept="pdfs" id="paper" />  </br>
            <input type="submit" name="resubmit" value="Resubmit" /> 
            </br>

            <?php
// Check if a file has been resubmitted
            if (isset($_FILES['paper']))
            {
// Make sure the file was sent without errors
                if ($_FILES['paper']['error'] == 0)
                {
                    //Open connection with database
                    $conn = @mysql_connect("localhost", "root", "") or die(mysql_error());
                    mysql_select_db("mydb") or die(mysql_error());
// Make sure an ID was passed
                    if (isset($_GET['paper_id']))
                    {
// Get the ID
                        $paperid = intval($_GET['paper_id']);
                    }
       //check if the size of the file exceeds 1 MB the maximum size then print a message for the user
          if($_FILES['paper']['size']> 1000000) {
             echo 'The size of the file exceeds the maximum size.Please choose a file of size less than 1 MB';
                 }
              else {
 //check if the file type is not pdf so print a message for the user since the allowed type is pdf only
               if($_FILES['paper']['type'] != 'application/pdf') {
                   echo 'Please only choose a pdf file, other types are not allowed to be submitted';
                            }
                //if its type is pdf so continue submitting
                      else {
                    //if the user click the submit button 
                    if (isset($_POST['resubmit']))
                    {

                        //select the version of the paper selected to resubmit it and fetch it.
                        $getVersionID = "SELECT `version_id` FROM `Version` WHERE `paper_id`= $paperid  ORDER BY version_id DESC";
                        $getversionid = mysql_query($getVersionID) or die(mysql_error());
                        $versionID = mysql_fetch_assoc($getversionid);
                        //get the new path of the paper
                        $target_path = "./paper/" . $_FILES['paper']['name'] . "_" . $versionID["version_id"];
                        move_uploaded_file($_FILES['paper']['tmp_name'], $target_path);
                       
                        // add the new version of the paper with its path
                        $AddNewVersion = "INSERT INTO `Version` (`paper_id`,`path`)
                     VALUES ($paperid,'{$target_path}')";
                        //take the autoincremented version id
                        include ("statics.php");
                        // Execute the query
                        mysql_query($AddNewVersion) or die(mysql_error());
                        $last_autoincrement_versionid = mysql_insert_id();
                        exec("pdftohtml -c -noframes " . escapeshellcmd($target_path));
                        iReformat($last_autoincrement_versionid);
                        // Check if it was successfull
                        if ($AddNewVersion)
                        { // if the query run successfully
                            echo 'Success! Your file was successfully resubmitted!';
                        }
                        else
                        {
                            echo 'Error! Failed to insert the file';
                        }


// Close the mysql connection
                        mysql_close($conn);
                    }}
                }}
                else
                {
                    echo 'There is an error while inserting the file!';
                
            }}
            ?>



    </body>
</html>





